<?php
/*
 * planets_class.php
 * 
 * Copyright 2013  <>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

Class planets {

	protected $planet_id;

/*
 *
 * name: __construct
 * @param
 * @return
 *
 */
	public function __construct($id) {
			   
		$this->planet_id = $id;

	}
    	
/*
 *
 * name: ListAllPlanets
 *
 *              returns an array with all the planet data stored in the database
 *              (planet_id, name, size, user and timestamp)
 * @param
 * 
 * @return
 *              $data: array of planets
 *              false: error
 */	
	public static function ListAllPlanets(){
		
		global $db;

		try {
			$query = "SELECT id, name, size, aud_usrmod, aud_timsmod FROM planets";
			
			foreach ($db->query($query) as $row) {
				$data[] = $row;
			}
		} catch(PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			return false;
		}
			   
		//If there is data in the db, returns the result of the query. If not returns false
		if(isset($data)){
				return $data;
		} else {
				return false;
		}

	}

/*
 *
 * name: CreatePlanet
 *
 *              Insert a new planet in the database
 *
 * @param:
 *              $name: name of the new planet
 *              $size: size of the new planet
 *              $user: user name inserting the new planet
 *
 * @return:
 *              true: in case of error (error msg)
 *              false: planet inserted with no errors
 */
	public static function CreatePlanet($name, $size, $user){
		
		global $db;
	   
		$query = "INSERT INTO planets (name, size, aud_usrmod) VALUES (?, ?, ?)";
		
		try {
			
			$stmt = $db->prepare($query);
			
			$stmt->execute(array($name, $size, $user));
	   
		} catch(PDOException $e) {
			
			//if duplicate error
			if ($e->getCode() == '23000') {
				return "Error: Duplicate planet.";
			}
			
			return "Error!: " . $e->getMessage();

		}
		return false;
	}
	

/*
 *
 * name: PlanetMap
 *
 *              Select all the deposits in a planet, returns a bidimensional table of
 *              deposits (data[x][y]=deposit data in (x,y))
 *
 * @param:
 *
 * @return:
 *              data[x][y]: deposit data array in (x,y)
 *              false: in case of error or no data
 */
	public function PlanetMap(){
			
		global $db;
	   
		//prepares the query
		$query = "SELECT x_coord, y_coord, material, size, id FROM deposits WHERE planet_id = ?";
		
		try {
			$stmt = $db->prepare($query);
			$stmt->bindParam(1,$this->planet_id);
			$stmt->execute();
			
			while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
				$data[$row["x_coord"]][$row["y_coord"]] = array( 'mat' => $row['material'],'size' => $row['size'],'id' => $row['id']);
			}
			
		} catch(PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			return false;
		}
	   
		//if there is data, return the data. If not, returns false
		if(isset($data)){
			   
				return $data;
			   
		} else {
				return false;
		}              
		   
	}
        

/*
 *
 * name: ReadPlanet
 *
 *              Select name and size of a planet
 *
 * @param:
 *
 * @return:
 *              $row: (name,size) array of planet_id
 *              false: in case of error or no data
 */
	public function ReadPlanet(){
			
		global $db;
	   
		$query = "SELECT name, size FROM planets WHERE id = ?";

		try {
			$stmt = $db->prepare($query);
			$stmt->bindParam(1,$this->planet_id);
			$stmt->execute();
			
			$row = $stmt->fetch(PDO::FETCH_ASSOC);

			
		} catch(PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			return false;
		}
	   
		//returns $row if set
		if(isset($row)){
			   
				return $row;
			   
		} else {
				return false;
		}      
		   
	}
        
/*
 *
 * name: AllDeposits
 *
 *              Select data of all the deposits in a planet. List-type return:
 *              returns a one-dimensional array (data[]) with the data of each deposit
 *
 * @param:
 *
 * @return:
 *              $data[]: array. Each row = associative array with deposit data
 *              false: error or no data
 */
	public function AllDeposits(){
		
		global $db;
	   
		$query = "SELECT x_coord, y_coord, material, size, id, user, time_mod FROM deposits WHERE ";
		$query .= "planet_id = ? ";
		$query .= "ORDER BY material, size ";

		try {
			$stmt = $db->prepare($query);
			$stmt->bindParam(1,$this->planet_id);
			$stmt->execute();
			
			$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

			
		} catch(PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			return false;
		}
			
	   
		//returns $data if set
		if(isset($data)){
				return $data;
			   
		} else {
				return false;
		}      
		   
	}
        	
}

?>
